package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.dto.GeoDTO;
import com.zz.parkingsystem.entity.Geomagnetism;
import com.zz.parkingsystem.service.PCGeoService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.AllGeoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "地磁信息展示")
@RequestMapping("admin/geo")
public class AdminGeoController {
    @Resource
    private PCGeoService pcGeoService;

    @ApiOperation(value = "展示所有地磁信息",notes = "根据路段名称以及地磁标号进行模糊查询")
    @PostMapping("/getAllGeo")
    public Result getAllGeo(@RequestBody GeoDTO geoDto){
        System.out.println(geoDto);
       geoDto.setStartPage((geoDto.getCurrentPage()-1)*geoDto.getPageSize());
        List<AllGeoVO> list = pcGeoService.getAllGeo(geoDto);
        Long total = pcGeoService.getCount(geoDto);
        Map map = new HashMap();
        map.put("list",list);
        map.put("total",total);
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "获取地磁信息，对应泊位表")
    @GetMapping("/allGeo")
    public Result allGeo(){
        List<Geomagnetism> geomagnetisms = pcGeoService.selAllGeo();
        return ResultGenerator.genSuccessResult(geomagnetisms);
    }

    @ApiOperation(value = "查询单个地磁信息",notes = "查询单个地磁信息")
    @GetMapping("{id}")
    public Result getGeoOne(@PathVariable("id") Long id){
        Geomagnetism geomagnetism = pcGeoService.selectByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(geomagnetism);
    }

    @ApiOperation(value = "增加地磁")
    @PostMapping("/add")
    public Result addGeoOne(@RequestBody Geomagnetism geomagnetism){
        int i = pcGeoService.insertSelective(geomagnetism);
        if(i>0){
            return ResultGenerator.genSuccessResult("添加成功");
        }else{
            return ResultGenerator.genFailResult("添加失败");
        }

    }

    @ApiOperation(value = "删除地磁")
    @DeleteMapping("{id}")
    public Result delGeoOne(@PathVariable("id") Long id){
        int i = pcGeoService.deleteByPrimaryKey(id);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "修改地磁")
    @PutMapping("")
    public Result updateGeoOne(@RequestBody Geomagnetism geomagnetism){
        System.out.println(geomagnetism);
        int i = pcGeoService.updateByPrimaryKeySelective(geomagnetism);
        return ResultGenerator.genSuccessResult();
    }
}
